package com.sl.discovery.modular;

import cn.hutool.core.collection.CollUtil;
import com.sl.discovery.api.SquareLakeDiscoveryClient;
import com.sl.discovery.api.domain.dto.SquareLakeServiceInstance;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;

import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class DiscoveryClientWrapper implements SquareLakeDiscoveryClient {
    @Resource
    private DiscoveryClient discoveryClient;

    @Override
    public List<SquareLakeServiceInstance> getInstances(String serviceId) {

        List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
        if (CollUtil.isEmpty(instances)) {
            return Collections.emptyList();
        }
        List<SquareLakeServiceInstance> list = instances.stream().map(v -> {
            SquareLakeServiceInstance serviceInstance = new SquareLakeServiceInstance();
            serviceInstance.setInstanceId(v.getInstanceId());
            serviceInstance.setServiceId(v.getServiceId());
            serviceInstance.setScheme(v.getScheme());
            serviceInstance.setHost(v.getHost());
            serviceInstance.setMetadata(v.getMetadata());
            serviceInstance.setPort(v.getPort());
            serviceInstance.setUri(v.getUri());
            serviceInstance.setSecure(v.isSecure());
            return serviceInstance;
        }).toList();
        return list;
    }

    @Override
    public List<String> getServices() {
        return discoveryClient.getServices();
    }
}
